﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Json;


/// <summary>
/// Summary description for Scrap
/// </summary>
public class Scrap
{

    ///Response From Orkut in JSON
    /*        "[" +
            "{" +
            "\"id\":\"1-scrp.g\"," +
            "\"data\":" +
                "{" +
                "\"filtered\":false," +
                "\"startIndex\":0," +
                "\"totalResults\":4," +
                "\"sorted\":false," +
                "\"list\":" +
                    "[" +
                    "{" +
                    "\"body\":\"Hiiiiii\"," +
                    "\"type\":\"PUBLIC_MESSAGE\"," +
                    "\"fromUserId\":\"00157859970414300231\"," +
                    "\"id\":\"603999420:1275348183:603999420:0\"," +
                    "\"time\":1275373383," +
                    "\"fromUserProfile\":" +
                        "{" +
                        "\"profileUrl\":\"/Profile.aspx?uid=9759041877583043299&mt=os\"," +
                        "\"isViewer\":true," +
                        "\"id\":\"00157859970414300231\"," +
                        "\"thumbnailUrl\":\"http://img2.orkut.com/images/small/1275349540/603999420/ep.jpg\"," +
                        "\"name\":" +
                            "{" +
                            "\"familyName\":\"Gangal\"," +
                            "\"givenName\":\"Harshit\"" +
                            "}" +
                        "}" +
                    "}," +
                    "{" +
                    "\"body\":\"Hiiiiii\"," +
                    "\"type\":\"PUBLIC_MESSAGE\"," +
                    "\"fromUserId\":\"00157859970414300231\"," +
                    "\"id\":\"603999420:1275348183:603999420:0\"," +
                    "\"time\":1275373383," +
                    "\"fromUserProfile\":" +
                        "{" +
                        "\"profileUrl\":\"/Profile.aspx?uid=9759041877583043299&mt=os\"," +
                        "\"isViewer\":true," +
                        "\"id\":\"00157859970414300231\"," +
                        "\"thumbnailUrl\":\"http://img2.orkut.com/images/small/1275349540/603999420/ep.jpg\"," +
                        "\"name\":" +
                            "{" +
                            "\"familyName\":\"Gangal\"," +
                            "\"givenName\":\"Harshit\"" +
                            "}" +
                        "}" +
                    "}" +
                    "]," +
                "\"updatedSince\":false" +
                "}" +
            "}" +
            "]";
    */


    public Scrap()
    {
        jParser = new JsonParser();
        no_of_messages = 0;
        socialMessage = null;
    }

    JsonParser jParser;
    SocialMessage[] socialMessage;
    int no_of_messages;

    /// <summary>
    /// Fetches the Messages
    /// </summary>
    /// <param name="response">JSON Response for the Get Messages Request</param>
    /// <param name="requiredMessages">Desired No. of Messages</param>
    /// <returns></returns>
    public SocialMessage[] fetch(string response, int requiredMessages)
    {
        String data = String.Empty;
        data = jParser.getData(true, response, "data");

        no_of_messages = jParser.getCount(data);

        string messageData = String.Empty;
        messageData = jParser.getData(false, data, "list");

        no_of_messages = (no_of_messages < requiredMessages) ? no_of_messages : requiredMessages;

        socialMessage = jParser.getMessages(messageData, no_of_messages);

        return socialMessage;
    }
}

